home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CScrollList 1.0 / CScrollList Classes / CStateArray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  2.5 KB  |  110 lines  |  [TEXT/KAHL]

  1. /*************************************************************************************
  2.  
  3.  CStateArray.c
  4.     
  5.         Implements an array of states (as shorts).
  6.     
  7.     SUPERCLASS = CArray
  8.     
  9.         © 1992 Dave Harkness
  10.  
  11. *************************************************************************************/
  12.  
  13.  
  14. #include "CStateArray.h"
  15.  
  16.  
  17.         /* Macros for calculating item offsets and addresses.    */
  18.         /* They are all one-based indices                        */
  19.  
  20. #define ASSERT_INDEX( index)    ASSERT( (index > 0)&&(index <= numItems))
  21. #define ITEM_OFFSET( index) ((index-1) * elementSize)
  22. #define ITEM_PTR( index) (&(*hItems)[ ITEM_OFFSET(index) ])
  23.  
  24.  
  25. /*************************************************************************************
  26.  IStateArray
  27. *************************************************************************************/
  28.  
  29. void
  30. CStateArray::IStateArray( long howMany, short fState)
  31.  
  32. {
  33.     CArray::IArray( sizeof( short));
  34.     
  35.     SetBlockSize( 10);
  36.     numItems = howMany;
  37.     Resize( howMany);
  38.     SetAllStates( fState);
  39.  
  40. }  /* CStateArray::IStateArray */
  41.  
  42.  
  43. /*************************************************************************************
  44.  SetState
  45.  
  46.     Set the state of the item at index to fState.
  47. *************************************************************************************/
  48.  
  49. void
  50. CStateArray::SetState( long index, short fState)
  51.  
  52. {
  53.     ASSERT_INDEX( index);
  54.     
  55.     *((short *)ITEM_PTR( index)) = fState;
  56.  
  57. }  /* CStateArray::SetState */
  58.  
  59.  
  60. /*************************************************************************************
  61.  ToggleState
  62.  
  63.     Toggle the state of the item at index.
  64. *************************************************************************************/
  65.  
  66. void
  67. CStateArray::ToggleState( long index)
  68.  
  69. {
  70.     ASSERT_INDEX( index);
  71.     
  72.     *((short *)ITEM_PTR( index)) = !(*((short *)ITEM_PTR( index)));
  73.  
  74. }  /* CStateArray::ToggleState */
  75.  
  76.  
  77. /*************************************************************************************
  78.  GetState
  79.  
  80.     Return the state of the item at index.
  81. *************************************************************************************/
  82.  
  83. short
  84. CStateArray::GetState( long index)
  85.  
  86. {
  87.     ASSERT_INDEX( index);
  88.     
  89.     return (*((short *)ITEM_PTR( index)));
  90.  
  91. }  /* CStateArray::GetState */
  92.  
  93.  
  94. /*************************************************************************************
  95.  SetAllStates
  96.  
  97.     Set the state of every item to fState.
  98. *************************************************************************************/
  99.  
  100. void
  101. CStateArray::SetAllStates( short fState)
  102.  
  103. {
  104.     long    index;
  105.     
  106.     for ( index = 1; index <= numItems; index++ )
  107.         *((short *)ITEM_PTR( index)) = fState;
  108.  
  109. }  /* CStateArray::SetAllStates */
  110.